/*
 * Project Beknyou
 * Copyright (c) 2010-2011 Saint Paul College, All Rights Reserved
 * Redistributions in source code form must reproduce the above
 * Copyright and this condition.
 * The contents of this file are subject to the GNU General Public
 * License, Version 2 (the "License"); you may not use this file
 * except in compliance with the License. A copy of the License is
 * available at http://www.opensource.org/licenses/gpl-license.php.
 */
package com.benkyou.client;

import com.benkyou.client.network.Chatter;
import com.benkyou.common.Avatar;
import com.benkyou.common.Player;
import com.jme3.system.AppSettings;
import com.jme3.system.JmeCanvasContext;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.KeyEvent;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

/**
 *
 * @author Austin Allman
 */

public class FormHandler {
    private GameClient gameClient;
    private AppSettings settings;
    private JmeCanvasContext ctx;
    private Dimension dim;
    private JFrame window;
    private JPanel panel;
    private LoginForm loginForm;
    private clientButtonForm clientButtonform;

    /**
     * Form Handler constructor
     * @param gameClient Used to interact with all the object in the game
     */
    public FormHandler(GameClient gameClient) {
        this.gameClient = gameClient;
    }
    
    /**
     * Init panel sets up the 3d game canvas aswell as the Jframe to attach the
     * game canvas and all the Swing buttons
     */
    public void initPanel() {
        //Creates the settings for the JME canvas
        settings = new AppSettings(true);
        settings.setWidth(640);
        settings.setHeight(480);

        //Sets the settings to the game client
        gameClient.setSettings(settings);
        gameClient.createCanvas(); // create canvas!
        ctx = (JmeCanvasContext) gameClient.getContext();
        ctx.setSystemListener(gameClient);
        dim = new Dimension(640, 480);
        ctx.getCanvas().setPreferredSize(dim);//Sets the canvas size

        //Creates the JFrame to hold our panel
        window = new JFrame("Benkyou Client");
        window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        //creates the panel to store all our game components
        panel = new JPanel(new BorderLayout()); // a panel
        //Creates the button form that has the text chat and class list;
        clientButtonform = new clientButtonForm();
        clientButtonform.getSendButton().addActionListener(new java.awt.event.ActionListener() {
             
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                gameClient.sendChatMessage(clientButtonform.getChatMessage());
            }
        });
        //adds support for hitting enter to send a chat message
        clientButtonform.getChatTextField().addKeyListener(new java.awt.event.KeyListener() {

            public void keyTyped(KeyEvent ke) {
            
            }

            public void keyPressed(KeyEvent ke) {
                //key code 10 is the enter key
                if(ke.getKeyCode() == 10)
                    gameClient.sendChatMessage(clientButtonform.getChatMessage());
            }

            public void keyReleased(KeyEvent ke) {
               
            }
        });


        window.setDefaultCloseOperation(window.EXIT_ON_CLOSE);
        createLoginPanel();


        window.add(panel);
        window.pack();
        window.setVisible(true);

    }

    /**
     * Handles the creattion of the login panel and attaches the action listener to the login button
     */
    public void createLoginPanel() {
        loginForm = new LoginForm();
        loginForm.getLoginButton().addActionListener(new java.awt.event.ActionListener() {

            public void actionPerformed(java.awt.event.ActionEvent evt) {
               
                gameClient.login(loginForm.getUsernameFieldText(), loginForm.getPasswordFieldText(),loginForm.getSelectedAvatarID());    
            }
        });
        panel.add(loginForm, BorderLayout.CENTER);
    }

    /**
     * Removes the login panel from the main JFrame
     */
    public void destroyLoginPanel() {
        panel.remove(loginForm);
        loginForm = null;
    }

    /**
     * Shows the Text area with the class list and send button
     */
    public void showClientForm() {
        panel.add(clientButtonform, BorderLayout.SOUTH);
    }

    /**
     * Shows the 3d canvas inside of the JFrame
     */
    public void showGamePanel() {
        panel.add(ctx.getCanvas(), BorderLayout.CENTER);
        gameClient.startCanvas();
    }

    /**
     * Used to start the game, Removes the login panel and shows the chat client
     * and game canvas
     */
    public void startGame() {
        destroyLoginPanel();
        showClientForm();
        showGamePanel();
        window.pack();
        window.repaint();
    }

    /**
     * Used to add a name to the list of users
     * @param name the name of the user to be added to the list
     */
    void addNewUser(Chatter chatter) {
        clientButtonform.addToUserList(chatter);
    }
    void removeUser(Chatter chatter){
        clientButtonform.removeFromUserList(chatter);
    }
    
    /**
     * Adds the chat message to the chat message box
     * @param name name of the user who sent the message
     * @param message the message the user sent
     */
    void addTextToChatBox(String name, String message) {
        clientButtonform.setChatText(name, message);
    }
    /**
     * Sends a chat message to the server
     * @param message the message to be sent 
     */
    public void sendMessage(String message) {
        gameClient.sendChatMessage(message);
    }
}
